
import { Injectable } from '@angular/core';
import { HttpHeaders, HttpClient, HttpParams } from '@angular/common/http';
import { Observable } from 'rxjs';

import { environment } from '../environments/environment';
import { Deliver } from '../models/Deliver';
import {Asset} from '../models/Asset';
import * as moment from 'moment';
@Injectable()
export class RepairService {

  private baseUrl = environment.apiUrl + 'api/repair';

  constructor(
    private http: HttpClient
  ) {}
  getList(current: number = 1, pageSize: number = environment.pageSize, options?: {assetName?: string, assetUserId?: string, assetDepartmentId?: string,
    assetType?: number, orders?: string[], position?: string,
    start_createDate?: Date, end_createDate?: Date}): Observable<{res: Deliver[], success: boolean, total: number}> {
    const paramsObj: any = {
      pageSize: String(pageSize),
      page: String(current),
    };
    if (options) {
      if (options.assetName) paramsObj['list[0].assetName'] = options.assetName;
      if (options.assetUserId) paramsObj['list[0].assetUserId'] = options.assetUserId;
      if (options.assetDepartmentId) paramsObj['list[0].assetDepartmentId'] = options.assetDepartmentId;
      if (options.position)  paramsObj['list[0].position'] = options.position;
      if (String(options.assetType) !== 'undefined') paramsObj['list[0].assetType'] = options.assetType;
      if (options.start_createDate) paramsObj.start_createDate = moment(options.start_createDate).format('YYYY-MM-DD');
      if (options.end_createDate) paramsObj.end_createDate = moment(options.end_createDate).format('YYYY-MM-DD');
    }
    const params = new HttpParams({ fromObject: paramsObj });
    return this.http.get<{res: Deliver[], success: boolean, total: number}>(`${this.baseUrl}/list`, { params: params });
  }
  create(deliver: Deliver): Observable<{success: boolean, res: Deliver}> {
    return this.http
      .post<{success: boolean, res: Deliver}>(`${this.baseUrl}/insert`, deliver);
  }

  update(deliver: Deliver): Observable<{res: Deliver, success: boolean}> {
    const url = `${this.baseUrl}/update`;
    return this.http
      .post<{res: Deliver, success: boolean}>(url, deliver);
  }
  getone(deliverNum): Observable<{success: boolean, res: Deliver}> {
    return this.http.get<{success: boolean, res: Deliver}>(`${this.baseUrl}/get?repairNum=${deliverNum}`);
  }
}
